import { createStore } from 'vuex'
import getters from './getters'

// 获取moudules 里面的js文件
const modulesFiles = require.context('./modules', true, /\.js$/)

const modules = modulesFiles.keys().reduce((modules, modulePath) => {
  // set './app.js' => 'app'
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')

  const value = modulesFiles(modulePath)

  // console.log('moduleName', moduleName) //  app user
  // console.log('value', value) // 对应文件里面的 states mutations actions

  modules[moduleName] = value.default
  return modules
}, {})

// console.log('modules', modules) // 整理成一个对象 {app: {…}, user: {…}, ...}

const store = createStore({
  modules,
  getters,
})

export default store
